#!/bin/sh

revision="eeprom2bin v0.2.0 (2023-06-14 by MIB-Wiki)"
# thanks to feralmib
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS > /dev/null
fi

if [ -f $TMP/reboot.mib ]; then
	echo "Reboot running..."
	return 2> /dev/null
fi

	case $1 in

	-bin) {

if [ -z $2 ]; then
echo -n "set input filename\n"
	return 2> /dev/null
else
	INFILE=$2
	[ ! -f $INFILE ] && echo -ne "Input file does not exist\n" && exit 1
fi

if [ -z $3 ]; then
	echo -ne "\n--- optional: set output filename\n"
	echo -ne "--- $MUVERSION-EEProm.bin will be used as default\n\n"
	OUTFILE=$BACKUPFOLDER/$MUVERSION-EEProm.bin
else
	OUTFILE="$3"
fi

autoincr() {

	f=$(basename $OUTFILE) # get file name
	d=$(dirname $OUTFILE) # get directory path
	ext=".bin" # required extension for output file

# Extract the file extension (if any), with preceeding '.'
if [[ "$f" == *.* ]] && [[ $ext = ".${f##*.}" ]]; then
	if [[ -e "$d/$f" ]]; then
		i=1
		f="${f%.*}"
		while [[ -e "$d/${f}_${i}${ext}" ]]; do
			let i++
		done
		f="${f}_${i}${ext}"
		OUTFILE="$d/$f"
	else
		echo -ne "File extension $ext does not match\nThis should not happen!\n"
		exit 1
	fi
else
	echo -ne "--- Output file does not fit required format "*"$ext\n"
	exit 1
fi
}

# TODO: make output path dynamic to current path and use input file name for output, just replace .bin

export LC_ALL=C
HEX=$($SED -rn 's/^0x\S+\W+(.*?)$/\1/p' "${INFILE}" | $SED -rn 's:\W*(\S\S)\W*:0x\1\n:pg' | $SED -rn '/^0x/p') 2>> $LOG
[ -f $OUTFILE ] && autoincr
echo "${HEX}" | awk '{printf("%c",strtonum($0))}' > "${OUTFILE}" 2>> $LOG
echo -ne "$(basename $OUTFILE) written to SD\n"

return 2> /dev/null
	};;

	# help or unknown parameter ------------------------------
	*) {
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" [OPTION] <INPUT file> <Optional: OUTPUT file>"
		echo ""
		echo "Options:"
		echo "        -bin			convert to bin"
		echo "        --help			show this help"
		echo ""
		echo "Note: Backup will only work in RCC bash!"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""

	};;

	esac

exit 0
